package com.translate.service.service.trans;

import org.springframework.stereotype.Service;

import com.translate.commons.bean.TransFieldTypeBean;
import com.translate.commons.bean.TransItemBean;
import com.translate.commons.enums.TransType;
import com.translate.commons.enums.TranslateFieldType;
import com.translate.commons.service.trans.DictFieldTranslateService;
import com.translate.commons.utils.ReflectDefinedUtil;
import com.translate.service.service.BaseUtilService;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 翻译给定的字段
 * 具体的翻译逻辑都在trans()
 * classFieldMap是做缓存用的,根据后面实际情况看换成啥把
 * @param <T>
 */
@Service
public class AutoTransService<T> extends BaseUtilService {

    private Map<String, Map<String, Field>> classFieldMap = new HashMap<String, Map<String, Field>>();
    private String tarLan = "1";

    private DictFieldTranslateService dictFieldTranslateService;

    public TransResultBean trans(TransItemBean transItem) {
        Object sourceData = transItem.getSourceData(); // 翻译数据
        Object targetData = transItem.getTargetData();
        Object oldData = transItem.getOldData(); // 历史数据
        if (sourceData == null) {
            return null;
        }
        Class clazz = sourceData.getClass();
        List<TransFieldTypeBean> fieldTypes = transItem.getFieldTypes(); // 需翻译的字段list
        if (!empty(fieldTypes)) {
            for (TransFieldTypeBean fieldType : fieldTypes) {
                String fieldName = fieldType.getFieldName(); // 字段名
                if (empty(fieldName)) {
                    continue;
                }
                Field field = getField(clazz, fieldName);
                if (field == null) {
                    continue;
                }
                Field fillField = field;
                String oldVal = ReflectDefinedUtil.getStr(oldData, field);
                String val = ReflectDefinedUtil.getStr(sourceData, field);
                String transVal = ReflectDefinedUtil.getStr(targetData, field);
                String transedVal = null;
                if (!empty(oldVal) && oldVal.equals(val) && !empty(transVal)) {
                    // 原来有值，且原来的值与新值没有变化，认为不需要翻译
                    // 这块的逻辑并不完善，目前可以不传old数据，不做执行
                    // 后面有时间再补充逻辑
                    transedVal = transVal;
                } else {
                    try {
                        transedVal = trans(val, fieldType); // 获取翻译之后的字段
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                ReflectDefinedUtil.setVal(sourceData, fillField, transedVal);
            }
        }

        Field bgyyField = getField(clazz, "lang"); // 获取报告类型
        ReflectDefinedUtil.setVal(sourceData, bgyyField, tarLan); // 默认是英文，后面再加其他语言的策略吧
        Field idField = getField(clazz, "id");
        Integer targetId = ReflectDefinedUtil.getInt(targetData, idField); // 适应翻译过的数据
        Integer chid = ReflectDefinedUtil.getInt(sourceData, idField);
        Field chidField = getField(clazz, "chid");
        ReflectDefinedUtil.setVal(sourceData, chidField, chid);

        if (targetId != null) { // 获取到之前翻译过的数据id，就是修改
            ReflectDefinedUtil.setVal(sourceData, idField, targetId);
            return TransResultBean.getUpdate(sourceData);
        }
        ReflectDefinedUtil.setVal(sourceData, idField, null);
        return TransResultBean.getAdd(sourceData);
    }

    /**
     *
     * @param val 翻译前数据
     * @param fieldType 翻译需要的数据（字段名，翻译规则，数据库类型坐标）
     * @return
     * @throws UnsupportedEncodingException
     */
    private String trans(String val, TransFieldTypeBean fieldType) throws UnsupportedEncodingException {
        if (empty(val)) {
            return null;
        }
        if (fieldType == null) {
            return null;
        }

        TransType transType = fieldType.getType();
        TranslateFieldType distType = fieldType.getDistType();

        if (transType == TransType.NULL) {
            return null;
        }
        if (transType == TransType.YES_NO) { // 翻译是否
            if (empty(val)) {
                return null;
            }
            if (val.equals("是")) {//
                val = "Yes";
            } else if (val.equals("否")) {//
                val = "No";
            } else {
                val = "Unknown";
            }
            return val;
        }
        if (transType == TransType.SEX) { // 翻译男女
            if (empty(val)) {
                return null;
            }
            if (val.equals("男")) {//
                val = "man";
            } else if (val.equals("女")) {//
                val = "woman";
            } else {
                val = "Unknown";
            }
            return val;
        }

        // 以下是翻译规则了
        // 自己根据实际情况添加
        // TransType枚举里是规则值
        if (transType == TransType.DIST) {
            if (distType == null) {
                return null;
            }
            return dictFieldTranslateService.getTranslateInfo(distType, val, tarLan); // 数据库翻译规则
        }

        if (transType == TransType.API) {
//            return apiFieldTranslateService.transByApi(val, distType); // 第三方翻译规则
        }

        return null;
    }

    // 缓存用对象用
    private Field getField(Class clazz, String fieldName) {
        Map<String, Field> fieldMap = classFieldMap.get(clazz.getName());
        if (fieldMap == null) {
            fieldMap = new HashMap<String, Field>();
        }
        Field field = fieldMap.get(fieldName);
        if (field != null) {
            return field;
        }
        field = ReflectDefinedUtil.getField(clazz, fieldName);
        fieldMap.put(fieldName, field);
        classFieldMap.put(clazz.getName(), fieldMap);
        return field;
    }

}

